package Views;

import Controllers.KitchenCTR;
import Models.*;
import Views.TableModels.KitchenTableModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * @author Tanja og Morten
 */
public class KitchenPicker extends javax.swing.JFrame implements ListSelectionListener, WindowListener
{
	private Kitchen chosenKitchen;
	private Item searchItem;
	private ItemPicker ip = null;
	private KitchenCTR controller = new KitchenCTR();

    public KitchenPicker()
	{
        initComponents();
		kitchenTable.getSelectionModel().addListSelectionListener(this);
		kitchenTable.addMouseListener(new MouseAdapter(){
			@Override
			public void mouseClicked(MouseEvent e){
				if (e.getClickCount() == 2)
				{
					if(kitchenTable.getSelectedRowCount() == 1)
					{
						chooseButtonActionPerformed(null);
					}
				}
			}
		 });
    }

	public Kitchen getChosenKitchen()
	{
		return this.chosenKitchen;
	}

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        nameLabel = new javax.swing.JLabel();
        nameTF = new javax.swing.JTextField();
        itemLabel = new javax.swing.JLabel();
        itemTF = new javax.swing.JTextField();
        findItemButton = new javax.swing.JButton();
        priceLabel = new javax.swing.JLabel();
        priceSP = new javax.swing.JSpinner();
        priceKrLabel = new javax.swing.JLabel();
        searchButton = new javax.swing.JButton();
        kitchenScrollPane = new javax.swing.JScrollPane();
        kitchenTable = new javax.swing.JTable();
        chooseButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Køkkenvælger");

        nameLabel.setText("Navn:");

        itemLabel.setText("Vare:");

        itemTF.setEnabled(false);

        findItemButton.setText("Find");
        findItemButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findItemButtonActionPerformed(evt);
            }
        });

        priceLabel.setText("Pris:");

        priceKrLabel.setText("kr.");

        searchButton.setText("Søg");
        searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButtonActionPerformed(evt);
            }
        });

        kitchenTable.setModel(new KitchenTableModel());
        kitchenScrollPane.setViewportView(kitchenTable);

        chooseButton.setText("Vælg");
        chooseButton.setEnabled(false);
        chooseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chooseButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Annuller");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, kitchenScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 457, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(layout.createSequentialGroup()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(layout.createSequentialGroup()
                                        .add(priceLabel)
                                        .add(18, 18, 18)
                                        .add(priceSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 95, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                        .add(priceKrLabel))
                                    .add(layout.createSequentialGroup()
                                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                                            .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                                                .add(nameLabel)
                                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                                .add(nameTF))
                                            .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                                                .add(itemLabel)
                                                .add(13, 13, 13)
                                                .add(itemTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 142, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                        .add(findItemButton))
                                    .add(searchButton))
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 90, Short.MAX_VALUE))
                            .add(layout.createSequentialGroup()
                                .add(chooseButton)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)))
                        .add(cancelButton)))
                .add(23, 23, 23))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(nameTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(nameLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(itemTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(itemLabel)
                    .add(findItemButton))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(priceSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(priceLabel)
                    .add(priceKrLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(searchButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(kitchenScrollPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 89, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(cancelButton)
                    .add(chooseButton))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed
		double price = Double.parseDouble(priceSP.getValue().toString());
		kitchenTable.setModel(
			new KitchenTableModel(
				this.controller.findKitchen(nameTF.getText(),
											price,
											null, null,
											null, null,
											this.searchItem)));
}//GEN-LAST:event_searchButtonActionPerformed

	private void findItemButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_findItemButtonActionPerformed
	{//GEN-HEADEREND:event_findItemButtonActionPerformed
		ip = new ItemPicker();
		ip.addWindowListener(this);
		ip.setVisible(true);
	}//GEN-LAST:event_findItemButtonActionPerformed

	private void chooseButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_chooseButtonActionPerformed
	{//GEN-HEADEREND:event_chooseButtonActionPerformed
		int chosenId = Integer.parseInt(kitchenTable.getValueAt(kitchenTable.getSelectedRow(), 0).toString());
		this.chosenKitchen = this.controller.getKitchen(chosenId);
		this.doClose();
	}//GEN-LAST:event_chooseButtonActionPerformed

	private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_cancelButtonActionPerformed
	{//GEN-HEADEREND:event_cancelButtonActionPerformed
		this.dispose();
	}//GEN-LAST:event_cancelButtonActionPerformed

	public void valueChanged(ListSelectionEvent e)
	{
		if(kitchenTable.getSelectedRowCount() == 1)
		{
			chooseButton.setEnabled(true);
		}
		else
		{
			chooseButton.setEnabled(false);
		}
	}

	public void windowClosing(WindowEvent e)
	{
		if(ip != null)
		{
			this.searchItem = ip.getChosenItem();
			if(this.searchItem != null)
			{
				itemTF.setText(this.searchItem.getName());
			}
			ip = null;
		}
	}

	public void doClose()
	{
        WindowEvent evt = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
        processWindowEvent(evt);
    }

	public void windowOpened(WindowEvent e) {}
	public void windowClosed(WindowEvent e) {}
	public void windowIconified(WindowEvent e) {}
	public void windowDeiconified(WindowEvent e) {}
	public void windowActivated(WindowEvent e) {}
	public void windowDeactivated(WindowEvent e) {}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton chooseButton;
    private javax.swing.JButton findItemButton;
    private javax.swing.JLabel itemLabel;
    private javax.swing.JTextField itemTF;
    private javax.swing.JScrollPane kitchenScrollPane;
    private javax.swing.JTable kitchenTable;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JTextField nameTF;
    private javax.swing.JLabel priceKrLabel;
    private javax.swing.JLabel priceLabel;
    private javax.swing.JSpinner priceSP;
    private javax.swing.JButton searchButton;
    // End of variables declaration//GEN-END:variables
}